package webgamemaker.Editor;

import java.util.ArrayList;

import webgamemaker.Common.Callback;
import webgamemaker.Common.Tile;
import webgamemaker.Common.TileData;
import webgamemaker.Common.TileSet;
import webgamemaker.Editor.HistoryManager.HistoryAction;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.KeyPressEvent;
import com.smartgwt.client.widgets.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class TileSetEditor extends HLayout
{
	public TileSet tileset;
	public TileSetPicker tilesetPicker;
	public TabSet tabSet;
	public Canvas tilesetCanvas;
	public TileSetEditor(TileSet tileset)
	{
		this.tileset = tileset;
		tabSet = new TabSet();
		Tab baseTileTab = new Tab();
		baseTileTab.setTitle("base");
		baseTileTab.setPane(new BaseTileCombiner(this));
		tabSet.addTab(baseTileTab);
		tabSet.setShowResizeBar(true);
		tilesetCanvas = new Canvas();
		tilesetCanvas.setCanFocus(true);
		tilesetPicker = new TileSetPicker(tileset);
		tilesetPicker.drawDefaultLayer = true;
		tilesetCanvas.addChild(tilesetPicker);
		tilesetCanvas.setShowResizeBar(true);
		tilesetCanvas.setOverflow(Overflow.AUTO);
		setMembers(tabSet, tilesetCanvas);
		tilesetPicker.addSelectCallback(new Callback()
		{
			public void onCall(Object src) 
			{
				tilesetCanvas.focus();
			}
		});
		tilesetCanvas.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event) 
			{
				System.out.println(event.getKeyName());
				if(tilesetPicker.selectX1 != -1 && tilesetPicker.selectY1 != -1)
				{
					String name = event.getKeyName();
					final ArrayList<TileData> tiles = tilesetPicker.tileset.tiles;
					final int x = tilesetPicker.selectX1;
					final int y = tilesetPicker.selectY1;
					final int width = tilesetPicker.selectX2 - x + 1;
					final int height = tilesetPicker.selectY2 - y + 1;
					final int decalX, decalY;
					if(name.equals("Arrow_Left"))
						decalX = -1;
					else if(name.equals("Arrow_Right"))
						decalX = 1;
					else decalX = 0;
					if(name.equals("Arrow_Up"))
						decalY = -1;
					else if(name.equals("Arrow_Down"))
						decalY = 1;
					else decalY = 0;
					final int width2 = (width+Math.abs(decalX));
					final int height2 = (height+Math.abs(decalY));
					final TileData oldTile[] = new TileData[width2*height2];
					for(int j = 0; j < height2; j++)
						for(int i = 0; i < width2; i++)
							oldTile[i+j*width2] = tiles.get(x+i+(decalX<0?decalX:0)+(y+j+(decalY<0?decalY:0))*tilesetPicker.nbTilePerLine);
					if(decalX != 0 || decalY != 0)
					{
						ProjectManager.getInstance().data.historyManager.makeAction(new HistoryAction()
						{
							public void redo() 
							{
								for(int j = 0; j < height2; j++)
									for(int i = 0; i < width2; i++)
										tiles.set(x+i+(decalX<0?decalX:0)+(y+j+(decalY<0?decalY:0))*tilesetPicker.nbTilePerLine, oldTile[((i+width2-decalX)%width2)+((j+height2-decalY)%height2)*width2]);
								tilesetPicker.selectX1+=decalX;
								tilesetPicker.selectY1+=decalY;
								tilesetPicker.selectX2+=decalX;
								tilesetPicker.selectY2+=decalY;
								tilesetPicker.redraw();
							}
							public void undo() 
							{
								for(int j = 0; j < height2; j++)
									for(int i = 0; i < width2; i++)
										tiles.set(x+i+(decalX<0?decalX:0)+(y+j+(decalY<0?decalY:0))*tilesetPicker.nbTilePerLine, oldTile[i+j*width2]);
								tilesetPicker.selectX1-=decalX;
								tilesetPicker.selectY1-=decalY;
								tilesetPicker.selectX2-=decalX;
								tilesetPicker.selectY2-=decalY;
								tilesetPicker.redraw();
							}
						});
					}
					else if(name.equals("Delete"))
					{
						ProjectManager.getInstance().data.historyManager.makeAction(new HistoryAction()
						{
							public void redo() 
							{
								for(int j = y+height-1; j >= y; j--)
									for(int i = x+width-1; i >= x; i--)
										tiles.remove(i+j*tilesetPicker.nbTilePerLine);
								tilesetPicker.redraw();
							}
							public void undo() 
							{
								for(int j = 0; j < height; j++)
									for(int i = 0; i < width; i++)
										tiles.add(x+i+(y+j)*tilesetPicker.nbTilePerLine,oldTile[i+j*width]);
								tilesetPicker.redraw();
							}
						});
					}
					else if(name.length() == 1 && name.charAt(0) >= '0' && name.charAt(0) <= '9')
					{
						final int index = Integer.parseInt(name);
						ProjectManager.getInstance().data.historyManager.makeAction(new HistoryAction()
						{
							public void redo() 
							{
								for(int j = 0; j < height; j++)
									for(int i = 0; i < width; i++)
										tiles.get(x+i+(y+j)*tilesetPicker.nbTilePerLine).default_layer = index;
								tilesetPicker.drawDefaultLayerText();
							}
							public void undo() 
							{
								for(int j = 0; j < height; j++)
									for(int i = 0; i < width; i++)
										tiles.get(x+i+(y+j)*tilesetPicker.nbTilePerLine).default_layer = oldTile[i+j*width].default_layer;
								tilesetPicker.drawDefaultLayerText();
							}
						});
					}
				}
			}
		
		});
	}
	
	public void updateTileSetPicker()
	{
		tilesetPicker.resetCanvas();
		tilesetPicker.redraw();
	}
}
